package com.lhys.handler;

import com.lhys.common.ApiResultMsg;
import com.lhys.common.ResultInfo;
import com.lhys.exception.CustomException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/5/12
 * Time: 下午 15:49
 * Description: 全局异常捕获
 */
@RestControllerAdvice
public class GlobalExceptionResolver {

    @ExceptionHandler(CustomException.class)
    public ApiResultMsg domainExceptionHandler(CustomException e) throws Exception {
        e.printStackTrace();
        return ApiResultMsg.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ApiResultMsg handleException(Exception e) {
        e.printStackTrace();
        return ApiResultMsg.error(ResultInfo.SERVER_ERROR);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ApiResultMsg handlerNotFoundException(NoHandlerFoundException e) {
        e.printStackTrace();
        return ApiResultMsg.error(ResultInfo.NOT_FONT);
    }

}